﻿Public Class Compras
    Dim porsentaje As Integer = 100

    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False
            'PanelArriba.Enabled = True
            PanelAbajo.Enabled = False

        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            modificar.Enabled = True
            eliminar.Enabled = True
            imprimir.Enabled = True
            'PanelArriba.Enabled = False
            PanelAbajo.Enabled = True
            'Panel3.Enabled = False
        End If
    End Sub
    Private Sub ComprasBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComprasBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.ComprasBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

    End Sub

    Private Sub Compras_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.comprasdetalle' table. You can move, or remove it, as needed.
        Me.ComprasdetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.comprasdetalle)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.comprasdetalle' table. You can move, or remove it, as needed.
        Me.ComprasdetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.comprasdetalle)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.unidadesdemedida' table. You can move, or remove it, as needed.
        Me.UnidadesdemedidaTableAdapter.Fill(Me.DbFireAndIceDataSet.unidadesdemedida)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
          ' Me.ComprasdetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.comprasdetalle)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.bodega' table. You can move, or remove it, as needed.
        Me.BodegaTableAdapter.Fill(Me.DbFireAndIceDataSet.bodega)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.tipospago' table. You can move, or remove it, as needed.
        Me.TipospagoTableAdapter.Fill(Me.DbFireAndIceDataSet.tipospago)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.proveedores' table. You can move, or remove it, as needed.
        Me.ProveedoresTableAdapter.Fill(Me.DbFireAndIceDataSet.proveedores)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.compras' table. You can move, or remove it, as needed.
        Me.ComprasTableAdapter.Fill(Me.DbFireAndIceDataSet.compras)
        IdcompraTextEdit.Properties.ReadOnly = True
        PanelDetalle.Enabled = False
        Paneltotales.Enabled = False
        Panelgrid.Enabled = True
        Panelencabezado.Enabled = False
        FechaDateTimePicker.Enabled = False
        DexcripcionTextEdit.Properties.ReadOnly = True
        CostounitarioTextEdit.EditValue = 0
        CantidadTextEdit.EditValue = 0
        TotallineaTextEdit.EditValue = 0
        TotalTextEdit.EditValue = 0
        DescuentoTextEdit1.EditValue = 0
        SubtotalTextEdit.EditValue = 0
        DescuentoTextEdit.EditValue = 0
        ImpuestoTextEdit.EditValue = 0
        TotalTextEdit.EditValue = 0
    End Sub

    Private Sub IdproductoTextEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdproductoTextEdit.EditValueChanged
        DexcripcionTextEdit.Text = IdproductoTextEdit.GetColumnValue("descripcion")
    End Sub

    Private Sub agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles agregar.Click
        BindingNavigatorAddNewItem.PerformClick()
        PanelDetalle.Enabled = False
        Paneltotales.Enabled = False
        Panelgrid.Enabled = False
        Panelencabezado.Enabled = True
        Paneltotales.Enabled = True
        Mostrar()
    End Sub

    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles detalle.Click
        If IdproveedorTextEdit.Text <> "" Then
            ErrorProvider1.SetError(IdproveedorTextEdit, Nothing)
            If IdtipopagoTextEdit.Text <> "" Then

                ErrorProvider1.SetError(IdtipopagoTextEdit, Nothing)
                detalle.Enabled = False
                PanelDetalle.Enabled = True
                ComprasBindingNavigatorSaveItem.PerformClick()
            Else

                ErrorProvider1.SetError(IdtipopagoTextEdit, "Debe seleccionar un tipo pago")
            End If

        Else
            ErrorProvider1.SetError(IdproveedorTextEdit, "Debe seleccionar un proveedor")
        End If
    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click

        If CostounitarioTextEdit.Text = "" Or CantidadTextEdit.Text = "" Or DescuentoTextEdit1.Text = "" Or TIPOUNIDAD.Text = "" Then
            If CostounitarioTextEdit.Text = "" Then
                ErrorProvider1.SetError(CostounitarioTextEdit, "Debe ingresar un Costo Unitario")
            Else
                ErrorProvider1.SetError(CostounitarioTextEdit, Nothing)
            End If

            If CantidadTextEdit.Text = "" Then
                ErrorProvider1.SetError(CantidadTextEdit, "Debe ingresar una Cantidad")
            Else
                ErrorProvider1.SetError(CantidadTextEdit, Nothing)
            End If

            If DescuentoTextEdit1.Text = "" Then
                ErrorProvider1.SetError(DescuentoTextEdit1, "Debe ingresar un Descuento")
            Else
                ErrorProvider1.SetError(DescuentoTextEdit1, Nothing)
            End If
            If TIPOUNIDAD.Text = "" Then
                ErrorProvider1.SetError(TIPOUNIDAD, "Debe ingresar un Tipo de Unidad")
            Else
                ErrorProvider1.SetError(TIPOUNIDAD, Nothing)
            End If
        Else
            ErrorProvider1.SetError(TIPOUNIDAD, Nothing)
            ErrorProvider1.SetError(DescuentoTextEdit1, Nothing)
            ErrorProvider1.SetError(CantidadTextEdit, Nothing)
            ErrorProvider1.SetError(CostounitarioTextEdit, Nothing)
            CANTIDAD.EditValue = TIPOUNIDAD.GetColumnValue("equivalenciauref") * CantidadTextEdit.EditValue
            TotallineaTextEdit.EditValue = (CostounitarioTextEdit.EditValue * CantidadTextEdit.EditValue) - ((CostounitarioTextEdit.EditValue * CantidadTextEdit.EditValue) * ((Val(DescuentoTextEdit1.EditValue)) / porsentaje))

            'ComprasdetalleTableAdapter.InsertQueryCompradetalle(IdcompraTextEdit.EditValue, IdproductoTextEdit.EditValue, CantidadTextEdit.EditValue, TotallineaTextEdit.EditValue, CostounitarioTextEdit.EditValue, DescuentoTextEdit1.EditValue, CANTIDAD.EditValue)
        End If
    End Sub

    Private Sub TIPOUNIDAD_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TIPOUNIDAD.EditValueChanged

        CANTIDAD.EditValue = TIPOUNIDAD.GetColumnValue("equivalenciauref") * CantidadTextEdit.EditValue
        MsgBox(CANTIDAD.Text)
    End Sub

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        Try
            For i As Integer = 0 To GridView1.DataRowCount - 1
                ComprasdetalleTableAdapter.DeleteQueryCompradetalle(Val(IdcompraTextEdit.Text))
            Next i
            ComprasTableAdapter.DeleteQueryCompras(IdcompraTextEdit.EditValue)
        Catch ex As Exception
        End Try


        Mostrar()
    End Sub

    Private Sub Compras_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed

        If cancelar.Enabled = True Then
            Try
                For i As Integer = 0 To GridView1.DataRowCount - 1
                    ComprasdetalleTableAdapter.DeleteQueryCompradetalle(Val(IdcompraTextEdit.Text))
                Next i
                ComprasTableAdapter.DeleteQueryCompras(IdcompraTextEdit.EditValue)
            Catch ex As Exception
            End Try
        End If

    End Sub



    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click
        ComprasdetalleTableAdapter.FillByCompradetalle(DbFireAndIceDataSet.comprasdetalle, IdcompraTextEdit.EditValue)

        Dim totalinea As Decimal = 0
        Dim total As Decimal = 0
        Dim descuento As Decimal = 0
        Dim imp As Decimal = 2
        Dim subtotal As Decimal = 0
        If DescuentoTextEdit.Text = "" Then
            DescuentoTextEdit.EditValue = 0
        End If
        If ImpuestoTextEdit.Text = "" Then
            ImpuestoTextEdit.EditValue = 0
        End If
        If IdproveedorTextEdit.Text <> "" Then
            ErrorProvider1.SetError(IdproveedorTextEdit, Nothing)
            If IdtipopagoTextEdit.Text <> "" Then
                 MsgBox("AKI VA BIEN")
                ErrorProvider1.SetError(IdtipopagoTextEdit, Nothing)
                detalle.Enabled = False
                PanelDetalle.Enabled = True

                For i As Integer = 0 To GridView2.DataRowCount - 1

                    totalinea = GridView2.GetRowCellValue(i, "totallinea")
                    subtotal = subtotal + totalinea
                Next i
                SubtotalTextEdit.EditValue = subtotal
                descuento = (Val(DescuentoTextEdit.EditValue) / Val(porsentaje))
                imp = (Val(ImpuestoTextEdit.EditValue) / Val(porsentaje))
                total = (subtotal + imp) - descuento
                TotalTextEdit.EditValue = total
                MsgBox(SubtotalTextEdit.Text)
                MsgBox(ImpuestoTextEdit.Text)
                MsgBox(DescuentoTextEdit.Text)

                MsgBox(TotallineaTextEdit.Text)
                ComprasBindingNavigatorSaveItem.PerformClick()
                'ComprasTableAdapter.InsertQueryCompras(IdcompraTextEdit.EditValue, IdproveedorTextEdit.EditValue, FechaDateTimePicker.Text, ObservacionTextEdit.EditValue, SubtotalTextEdit.EditValue, DescuentoTextEdit.EditValue, TotalTextEdit.EditValue, IdtipopagoTextEdit.EditValue, ImpuestoTextEdit.EditValue, 1)
                Mostrar()

            Else
                ErrorProvider1.SetError(IdtipopagoTextEdit, "Debe seleccionar un tipo pago")
            End If

        Else
            ErrorProvider1.SetError(IdproveedorTextEdit, "Debe seleccionar un proveedor")
        End If


    End Sub

End Class
